package com.examination.system.administrator.service.impl;

import com.alibaba.fastjson.JSON;
import com.examination.framework.model.response.CommonCode;
import com.examination.framework.model.response.QueryResponseResult;
import com.examination.framework.model.response.QueryResult;
import com.examination.framework.utils.VisitURLUtil;
import com.examination.system.administration.Qreaschina;
import com.examination.system.administration.response.ProvinceResponse;
import com.examination.system.administrator.dao.QreaschinMapper;
import com.examination.system.administrator.dao.QreaschinaRepository;
import com.examination.system.administrator.service.QreaschinaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 寇淇芮
 * @Date 2019/8/29 9:10
 */
@Service
public class QreaschinaServiceImpl implements QreaschinaService {
    @Value("${administrator.qreaschina.appkey}")
    private String  appkey;

    @Value("${administrator.qreaschina.obtainProvince.url}")
    private String obtainProvinceUrl;

    @Value("${administrator.qreaschina.county.url}")
    private String countyUrl;

    @Value("${administrator.qreaschina.toObtain.url}")
    private String toObtainUrl;

    @Autowired
    private  QreaschinaRepository qreaschinaRepository;

    @Autowired
    private QreaschinMapper qreaschinMapper;

    @Override
    public QueryResponseResult getObtainProvince() {
        return returnQueryResponseResult("0",obtainProvinceUrl+"?"+appkey);
    }
    private QueryResponseResult returnQueryResponseResult(String id,String url  ){
        List<Qreaschina> qreaschinas = new ArrayList<>();
        qreaschinas  = getQreaschinaByParentid(id);
        if(qreaschinas.size()==0){
            try {
                qreaschinas =  getQreaschinaUrl(url);
                if(qreaschinas.size()>0){
                    addToDatabase(qreaschinas);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        QueryResult queryResult = new QueryResult();
        queryResult.setTotal(Long.valueOf(qreaschinas.size()));
        queryResult.setList(qreaschinas);
        return qreaschinas.size() == 0?new QueryResponseResult(CommonCode.FAIL,queryResult):new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }
    /**
     * 把数据添加到数据库里面，
     * @param qreaschinas
     */
    @Transient
    private void addToDatabase(List<Qreaschina> qreaschinas) {
        int insertList = qreaschinMapper.insertList(qreaschinas);
    }

    /**
     * 获取数据
     * @param urlStr
     * @return
     * @throws Exception
     */
    private List<Qreaschina> getQreaschinaUrl(String urlStr) throws Exception {
        ProvinceResponse<Qreaschina>  qreaschina = JSON.parseObject(VisitURLUtil.getData(urlStr,"UTF-8"),ProvinceResponse .class);
        return qreaschina.getResult();

    }
    @Transient
    private List<Qreaschina> getQreaschinaByParentid(String parentId) {
       return qreaschinaRepository.findByParentid(parentId);
    }

    @Override
    public QueryResponseResult getToObtain(String id) {
        return this.returnQueryResponseResult(id,toObtainUrl+"?parentid="+id+"&"+appkey);
    }

    @Override
    public QueryResponseResult  getCounty(String id) {
        return this.returnQueryResponseResult(id,countyUrl+"?parentid="+id+"&"+appkey);
    }
}
